package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.exception.TeacherNameNotExistException;
import com.TeacherInfoServer.listener.TeacherListener;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.TeacherSearchDTO;
import com.TeacherInfoServer.pojo.DTO.TeacherDTO;
import com.TeacherInfoServer.pojo.entity.Teacher;
import com.TeacherInfoServer.pojo.excelImport.TeacherExcel;
import com.TeacherInfoServer.common.service.TeacherService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.TeacherInfoServer.common.Result.success;


/**
 * 教师信息控制器

 */
@Slf4j
@RestController
@RequestMapping("/s1/teacher")
@Tag(name = "教师信息控制器",description = "用于对教师信息的控制")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeacherListener teacherListener;

    @GetMapping(value = "/getOne",params = "teacher_id")
    public Result<TeacherDTO> getOne(@RequestParam String teacher_id){
        return teacherService.getOne(teacher_id);
    }
    @Operation(summary = "根据id获取教师姓名",description = "用于远程调用获取教师姓名")
    @GetMapping(value = "/getName")
    public Result<String> getName(@RequestParam String teacherId){
        QueryWrapper<Teacher> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(Teacher::getTeacherId,teacherId);
        Teacher teacher = teacherService.getOne(queryWrapper);
        if (teacher == null){
           throw new TeacherNameNotExistException();
        }
        return Result.success(null,teacher.getTeacherName());
    }

    @Operation(summary = "获取教师信息列表",description = "分页获取教师信息列表")
    @GetMapping("/getList")
    public Result<Map<String,Object>> getAllList(@RequestParam("pages") @Parameter(description = "当前页数") Integer pages,
                                                 @RequestParam("size") @Parameter(description = "当前页数显示信息的数量") Integer size){
        return success("获取教师列表成功",teacherService.getTeacherList(pages,size));
    }
    @Operation(summary = "模糊查询获取教师基本信息列表",description = "模糊查询分页获取教师基本信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody TeacherSearchDTO teacherSearchDTO){
        return success("获取教师基本列表成功",teacherService.searchTeacherList(teacherSearchDTO));
    }
    @Operation(summary = "添加单个教师信息",description = "添加单个教师信息")
    @PostMapping("/addTeacher")
    public Result<?> addTeacher(@RequestBody TeacherDTO teacherDTO){
        teacherService.addTeacher(teacherDTO);
        return success("添加教师信息成功");
    }

    @Operation(summary = "Excel批量导入教师信息",description = "Excel批量导出教师信息")
    @PostMapping("/addByBatch")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result<Map<String,Integer>> addBatch(@RequestParam("file")@Parameter(description = "添加Excel文件") MultipartFile file) throws IOException {
        teacherListener.getTeacherList().clear();
        EasyExcel.read(file.getInputStream(), TeacherExcel.class,teacherListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",teacherListener.getRecord());
        return success(200,"批量导入成功",map);
    }
    @Operation(summary = "Excel导出教师信息",description = "Excel导出教师信息")
    @GetMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response){
      teacherService.exportInfo(response);
    }


    @Operation(summary = "更新单个教师信息",description = "更新单个教师信息")
    @PutMapping("/updateTeacher")
    public Result<?> updateTeacher(@RequestBody TeacherDTO teacherDTO){
        teacherService.updateTeacher(teacherDTO);
        return success("修改教师信息成功");
    }
    @Operation(summary = "根据工号批量删除教师信息",description = "根据工号批量删除教师信息")
    @DeleteMapping("/deleteTeacher")
    public Result<?> deleteTeacher(@RequestParam("teacherIds") @Parameter(description = "教师工号列表") List<String> teacherIds){
        boolean isDelete=teacherService.deleteTeacher(teacherIds);
        if (isDelete) {
            return success("删除成功");
        }
        else {
            return Result.fail("没有相应教师，删除失败");
        }
    }

    @Operation(summary = "获取教师姓名列表",description = "获取所有教师姓名列表")
    @GetMapping("/getTeacherNameList")
    public Result<Map<String,Object>> getTeacherNameList(@RequestParam("currentPage") @Parameter(description = "查询页码") Integer currentPage,
                                                         @RequestParam("pageSize") @Parameter(description = "页大小") Integer pageSize,
                                                         @RequestParam(required = false) String teacherName){
        return success("获取教师姓名列表成功",teacherService.getTeacherNameList(currentPage,pageSize,teacherName));
    }
}
